#include <iostream>
#include <sys/wait.h>
#include <sys/stat.h>
#include <string>
#include <cerrno>
#include <cstring>
using namespace std;
#define SIZE 1024
void Write(int fd){
    static int cnt = 0;
    // string message = "The id is:";
    // message += to_string(getpid());
    // message += ",  ";
    // message += "the number is:";
    // message += to_string(++cnt);
    // write(fd, message.c_str(), message.size());
    write(fd, "a", 1);
    cout << ++cnt << endl;
}
void Read(int fd){
    char buffer[SIZE];
    int n = read(fd, buffer, sizeof(buffer));
    buffer[n] = 0;
    cout << buffer << endl;
}
int main()
{
    int Pipe[2];
    int n = pipe(Pipe);
    if(n)
    {
        cerr << "errno:" << errno << "->" << "errstring:" << strerror(errno) << endl;
        return 1;
    }
    pid_t id = fork();
    if(id == 0)
    {
        close(Pipe[0]);
        while(1)
        {
            Write(Pipe[1]);
            //sleep(2);
        }
    }
    close(Pipe[1]);
    close(Pipe[0]);
    //while(1)
    //{
        //Read(Pipe[0]);   
        
    //}
    int status = 0;
    int ID = waitpid(id, &status, 0);
    cout << (status & 255) << endl;
    cout << ((status >> 9) & 127) << endl;
}